package cn.zmall.front.controller;

import cn.zmall.common.pojo.Result;
import cn.zmall.common.utils.ResultUtil;
import cn.zmall.manager.dto.front.Member;
import cn.zmall.manager.dto.front.MemberLoginRegist;
import cn.zmall.sso.service.LoginService;
import cn.zmall.sso.service.RegisterService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author BinBini
 * @Date 2020/10/28 11:09
 * @Version 1.0
 */
@RestController
@Api(value = "会员注册登录")
public class MemberController {

    @Reference
    private RegisterService registerService;
    @Reference
    private LoginService loginService;

    @RequestMapping(value = "/member/login", method = RequestMethod.POST)
    @ApiOperation(value = "用户登录")
    public Result<Member> login(@RequestBody MemberLoginRegist memberLoginRegist,
                                HttpServletRequest request) {
        Member member = loginService.userLogin(memberLoginRegist.getUserName(), memberLoginRegist.getUserPwd());
        return new ResultUtil<Member>().setData(member);
    }

    @RequestMapping(value = "/member/login/wx", method = RequestMethod.GET)
    @ApiOperation(value = "微信小程序登录")
    public Result<Member> wxLogin(String wxId) {
        Member member = loginService.wxLogin(wxId);
        return new ResultUtil<Member>().setData(member);
    }

    @RequestMapping(value = "/member/checkLogin", method = RequestMethod.GET)
    @ApiOperation(value = "判断用户是否登录")
    public Result<Member> checkLogin(@RequestParam(defaultValue = "") String token) {
        Member member = loginService.getUserByToken(token);
        return new ResultUtil<Member>().setData(member);
    }

    @RequestMapping(value = "/member/logout", method = RequestMethod.GET)
    @ApiOperation(value = "退出登录")
    public Result<Object> logout(@RequestParam(defaultValue = "") String token) {
        loginService.logout(token);
        return new ResultUtil<Object>().setData(null);
    }

    @RequestMapping(value = "/member/register", method = RequestMethod.POST)
    @ApiOperation(value = "用户注册")
    public Result<Object> register(@RequestBody MemberLoginRegist memberLoginRegist,
                                   HttpServletRequest request) {
        int result = registerService.register(memberLoginRegist.getUserName(),
                memberLoginRegist.getUserPwd(),
                memberLoginRegist.getPhone(),
                memberLoginRegist.getEmail());
        if (result == 0) {
            return new ResultUtil<Object>().setErrorMsg("该用户名已被注册");
        } else if (result == -1) {
            return new ResultUtil<Object>().setErrorMsg("用户名密码不能为空");
        }
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/menber/getuserinfo",method = RequestMethod.GET)
    @ApiOperation(value = "获取用户信息")
    public Result<Member> getUserInfo(@RequestParam(defaultValue = "")Long userId){
        Member result = loginService.getUserInfo(userId);
        return new ResultUtil<Member>().setData(result);
    }
}
